package com.longqin.wechat.service;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.auth0.jwt.interfaces.Claim;
import com.longqin.wechat.entity.MyException;
import com.longqin.wechat.jwt.JWTUtil;
import com.longqin.wechat.util.ResponseEnum;


/**通用接口
 * @author longqin
 *
 */
public abstract class CommSerive {
	
	@Resource
	private RestTemplate restTemplate;
	
	/**post请求返回结果的json字符串
	 * @param url 请求地址
	 * @param bodyData 请求对象
	 * @param requestHeaders 请求头格式
	 * @param errorTip 错误提示
	 * @return
	 * @throws Exception
	 */
	protected String postRest(String url, Object bodyData, HttpHeaders requestHeaders, String errorTip) throws Exception {
		HttpEntity<Object> requestEntity = new HttpEntity<>(bodyData, requestHeaders);
		ResponseEntity<String> entity = restTemplate.postForEntity(url, requestEntity, String.class);	
		return getRestResultBody(entity, errorTip);
	}
	
	/**post请求
	 * @param url 请求地址
	 * @param bodyData 请求体
	 * @param vars 请求参数
	 * @param errorTip 错误提示
	 * @return
	 * @throws Exception
	 */
	protected String postRest(String url, Object bodyData, Map<String, String> vars, String errorTip) throws Exception {
		HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
		HttpEntity<Object> requestEntity = new HttpEntity<>(bodyData, requestHeaders);
		ResponseEntity<String> entity = restTemplate.postForEntity(url, requestEntity, String.class,vars);	
		return getRestResultBody(entity, errorTip);
	}
	
	/**get请求返回结果的json字符串
	 * @param url 请求地址
	 * @param vars 参数值
	 * @return
	 * @throws Exception
	 */
	protected String getRest(String url, Map<String, String> vars) throws Exception {
		ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class,vars);
		return getRestResultBody(entity, "请求接口失败");
	}
	
	/**获取返回值
	 * @param entity
	 * @return
	 */
	private String getRestResultBody(ResponseEntity<String> entity, String errorTip){
		HttpStatus statusCode = entity.getStatusCode();
	    if(statusCode == HttpStatus.OK) { //调用成功
	    	return entity.getBody().toString();
	    }else throw new MyException(ResponseEnum.ERROR.getCode(), errorTip);
	}

	/**获取账号信息
	 * Claims目前主要包含账号id,账号名称,账号级别，账号所属机构编码
	 * 用于查询时数据权限的控制
	 * @return 
	 */
	protected Map<String, Claim> getUserClaims(){
		PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
		String token = (String)principals.getPrimaryPrincipal();
		return JWTUtil.getJWTClaim(token);
	}
	
	/**
	 * @Description: 获取账号id
	 * @return
	 * 
	 */
	protected Integer getUserId(){
		return getUserClaims().get("userId").asInt();
	}
	
	/**
	 * @Description: 获取组织机构id
	 * @return
	 * 
	 */
	protected Integer getOrgId(){
		return getUserClaims().get("orgId").asInt();
	}
	
}
